<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务管理 - 校园互助系统</title>
    <style>
        :root {
            --primary-color: #4285f4;
            --secondary-color: #34a853;
            --accent-color: #fbbc05;
            --danger-color: #ea4335;
            --light-color: #f8f9fa;
            --dark-color: #202124;
        }
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: var(--dark-color);
            background-color: #f5f5f5;
            padding: 0;
            margin: 0;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        header {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 2rem 0;
            text-align: center;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        h1 {
            font-size: 2.5rem;
            margin-bottom: 0.5rem;
        }
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }
        .nav {
            display: flex;
            justify-content: center;
            background-color: white;
            padding: 10px 0;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .nav a {
            color: var(--dark-color);
            text-decoration: none;
            padding: 8px 16px;
            margin: 0 5px;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .nav a:hover, .nav a.active {
            background-color: var(--light-color);
            color: var(--primary-color);
        }
        .nav a.active {
            font-weight: bold;
        }
        .task-container {
            margin-top: 30px;
        }
        .task-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .task-header h2 {
            color: var(--primary-color);
        }
        .btn {
            display: inline-block;
            background-color: var(--primary-color);
            color: white;
            padding: 8px 16px;
            border-radius: 4px;
            text-decoration: none;
            transition: background-color 0.3s ease;
            border: none;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #3367d6;
        }
        .btn-secondary {
            background-color: var(--secondary-color);
        }
        .btn-secondary:hover {
            background-color: #2d9047;
        }
        .btn-accent {
            background-color: var(--accent-color);
        }
        .btn-accent:hover {
            background-color: #e0a800;
        }
        .task-list {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .task-item {
            padding: 15px 20px;
            border-bottom: 1px solid var(--light-color);
            transition: background-color 0.3s;
        }
        .task-item:last-child {
            border-bottom: none;
        }
        .task-item:hover {
            background-color: var(--light-color);
        }
        .task-title {
            font-weight: bold;
            color: var(--primary-color);
            margin-bottom: 5px;
            font-size: 1.1rem;
        }
        .task-description {
            color: #666;
            margin-bottom: 10px;
        }
        .task-meta {
            display: flex;
            justify-content: space-between;
            font-size: 0.9rem;
            color: #888;
        }
        .task-status {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 0.8rem;
            font-weight: bold;
        }
        .status-pending {
            background-color: #e8f0fe;
            color: var(--primary-color);
        }
        .status-accepted {
            background-color: #e6f4ea;
            color: var(--secondary-color);
        }
        .status-completed {
            background-color: #fef7e0;
            color: var(--accent-color);
        }
        .task-actions {
            margin-top: 10px;
        }
        .task-actions .btn {
            padding: 4px 8px;
            font-size: 0.9rem;
            margin-right: 5px;
        }
        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #888;
        }
        .empty-state i {
            font-size: 3rem;
            margin-bottom: 10px;
            color: #ddd;
        }
        footer {
            text-align: center;
            margin-top: 50px;
            padding: 20px;
            color: #666;
            font-size: 0.9rem;
        }
        #task-loading {
            text-align: center;
            padding: 20px;
        }
        .loading-spinner {
            display: inline-block;
            width: 40px;
            height: 40px;
            border: 4px solid rgba(0, 0, 0, 0.1);
            border-radius: 50%;
            border-top-color: var(--primary-color);
            animation: spin 1s ease-in-out infinite;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        /* 美化发布任务弹窗 */
        #publish-modal {
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            background: rgba(0,0,0,0.25);
            z-index: 1001;
            display: none;
        }
        #publish-modal .modal-content {
            background: #fff;
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.18);
            min-width: 340px;
            max-width: 95vw;
            margin: 80px auto 0 auto;
            padding: 32px 28px 24px 28px;
            position: relative;
            animation: fadeIn 0.2s;
        }
        @keyframes fadeIn { from { opacity: 0; transform: translateY(-30px);} to { opacity: 1; transform: none; } }
        #publish-modal .modal-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        #publish-modal .modal-header h3 {
            margin: 0;
            font-size: 1.3rem;
            font-weight: bold;
        }
        #publish-modal .close {
            background: none;
            border: none;
            font-size: 1.7rem;
            color: #888;
            cursor: pointer;
            margin-left: 8px;
            transition: color 0.2s;
        }
        #publish-modal .close:hover {
            color: #f44336;
        }
        #publish-modal .form-group {
            margin-bottom: 18px;
        }
        #publish-modal label {
            display: block;
            margin-bottom: 6px;
            font-weight: 500;
        }
        #publish-modal input,
        #publish-modal textarea {
            width: 100%;
            padding: 8px 10px;
            border: 1px solid #d0d0d0;
            border-radius: 6px;
            font-size: 1rem;
            margin-bottom: 2px;
            transition: border 0.2s;
        }
        #publish-modal input:focus,
        #publish-modal textarea:focus {
            border: 1.5px solid #1976d2;
            outline: none;
        }
        #publish-modal textarea {
            min-height: 60px;
            resize: vertical;
        }
        #publish-modal .form-actions {
            display: flex;
            justify-content: flex-end;
            gap: 12px;
            margin-top: 18px;
        }
        #publish-modal .btn {
            padding: 8px 22px;
            border: none;
            border-radius: 6px;
            font-size: 1rem;
            cursor: pointer;
            transition: background 0.2s;
        }
        #publish-modal .btn-cancel {
            background: #e0e0e0;
            color: #333;
        }
        #publish-modal .btn-cancel:hover {
            background: #bdbdbd;
        }
        #publish-modal .btn-primary {
            background: #1976d2;
            color: #fff;
        }
        #publish-modal .btn-primary:hover {
            background: #1565c0;
        }
        /* 补充btn-danger样式 */
        .btn-danger { background: #ea4335; color: #fff; } .btn-danger:hover { background: #c62828; }
        /* 任务详情弹窗样式 */
        #task-detail-modal {
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            background: rgba(0,0,0,0.25);
            z-index: 1002;
            display: none;
        }
        #task-detail-modal .modal-content {
            background: #fff;
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.18);
            min-width: 340px;
            max-width: 95vw;
            margin: 80px auto 0 auto;
            padding: 32px 28px 24px 28px;
            position: relative;
            animation: fadeIn 0.2s;
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <h1>任务管理</h1>
            <p class="subtitle">浏览、发布、接受和完成校园互助任务</p>
        </div>
    </header>
    <div class="nav">
        <a href="/campus-help/" class="nav-link">首页</a>
        <a href="/campus-help/tasks.html" class="nav-link active">任务管理</a>
        <a href="/campus-help/actuator/health" class="nav-link">系统状态</a>
        <a href="/campus-help/user.html" class="nav-link">用户服务</a>
    </div>
    <div class="container">
        <div class="task-container">
            <div class="task-header">
                <h2>所有任务</h2>
                <div>
                    <button id="refresh-btn" class="btn">刷新列表</button>
                    <button id="open-publish-modal" class="btn btn-secondary">发布任务</button>
                </div>
            </div>
            <div id="task-list" class="task-list">
                <div id="task-loading">
                    <div class="loading-spinner"></div>
                    <p>正在加载任务...</p>
                </div>
            </div>
        </div>
        <footer>
            <p>校园互助系统 &copy; 2025 | <a href="/campus-help/">返回首页</a></p>
        </footer>
    </div>
    <!-- 发布任务模态框 -->
    <div id="publish-modal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>发布新任务</h3>
                <span class="close">&times;</span>
            </div>
            <div class="modal-body">
                <form id="publish-task-form">
                    <div class="form-group">
                        <label for="task-title">任务标题</label>
                        <input type="text" id="task-title" name="title" required>
                    </div>
                    <div class="form-group">
                        <label for="task-description">任务描述</label>
                        <textarea id="task-description" name="description" rows="4" required></textarea>
                    </div>
                    <div class="form-group">
                        <label for="task-reward">报酬 (元)</label>
                        <input type="number" id="task-reward" name="reward" min="0" step="0.01" required>
                    </div>
                    <div class="form-group">
                        <label for="task-deadline">截止时间</label>
                        <input type="datetime-local" id="task-deadline" name="deadline" required>
                    </div>
                    <div class="form-group">
                        <label for="publisher-id" style="display:none;">发布者学号</label>
                        <input type="text" id="publisher-id" name="publisherId" style="display:none;" readonly>
                    </div>
                    <div class="form-actions">
                        <button type="button" class="btn btn-cancel" id="cancel-publish">取消</button>
                        <button type="submit" class="btn btn-primary">发布任务</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- 任务详情模态框 -->
    <div id="task-detail-modal" class="modal" style="display:none;z-index:1002;">
        <div class="modal-content" style="max-width:500px;">
            <div class="modal-header">
                <h3>任务详情</h3>
                <span class="close" id="close-detail-modal">&times;</span>
            </div>
            <div class="modal-body" id="task-detail-body">
                <!-- 动态填充 -->
            </div>
        </div>
    </div>
    <script>
        // 登录校验
        const user = JSON.parse(sessionStorage.getItem('user') || 'null');
        if (!user) {
            window.location.href = 'login.html';
        }
        document.addEventListener('DOMContentLoaded', function() {
            fetchTasks();
            document.getElementById('refresh-btn').addEventListener('click', function() {
                fetchTasks();
            });
        });
        function fetchTasks() {
            const taskList = document.getElementById('task-list');
            taskList.innerHTML = '<div id="task-loading"><div class="loading-spinner"></div><p>正在加载任务...</p></div>';
            const apiUrl = '/campus-help/api/tasks/list';
            fetch(apiUrl)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayTasks(data.data);
                    } else {
                        showError(data.message);
                    }
                })
                .catch(error => {
                    showError('获取任务列表失败，请检查API是否正常工作');
                    console.error('获取任务列表出错:', error);
                });
        }
        function displayTasks(tasks) {
            const taskList = document.getElementById('task-list');
            taskList.innerHTML = '';
            if (!Array.isArray(tasks)) {
                showError('任务数据格式错误');
                console.error('任务数据不是数组:', tasks);
                return;
            }
            if (tasks.length === 0) {
                taskList.innerHTML = '<div class="empty-state"><div>X</div><p>暂无任务</p><p>点击\"发布任务\"按钮创建新任务</p></div>';
                return;
            }
            tasks.forEach(task => {
                try {
                    const taskItem = document.createElement('div');
                    taskItem.className = 'task-item';
                    let statusClass = 'status-pending';
                    let statusText = '待接受';
                    if (task.status === 'ACCEPTED') {
                        statusClass = 'status-accepted';
                        statusText = '进行中';
                    } else if (task.status === 'COMPLETED') {
                        statusClass = 'status-completed';
                        statusText = '已完成';
                    }
                    let deadline = new Date(task.deadline && typeof task.deadline === 'string' ? task.deadline.replace('T', ' ').replace(/-/g, '/') : task.deadline);
                    let formattedDeadline;
                    if (isNaN(deadline.getTime())) {
                        formattedDeadline = task.deadline || '';
                    } else {
                        formattedDeadline = deadline.toLocaleDateString('zh-CN') + ' ' + deadline.toLocaleTimeString('zh-CN', {hour: '2-digit', minute:'2-digit'});
                    }
                    let actionsHtml = `<button class='btn' onclick='viewTaskDetail("${task.taskId}")'>查看详情</button>`;
                    // 当前登录用户
                    const currentUserId = localStorage.getItem('studentId');
                    // 只有自己发布的任务显示"撤回任务"按钮，且任务为PENDING或ACCEPTED
                    if ((task.status === 'PENDING' || task.status === 'ACCEPTED') && currentUserId && task.publisherId === currentUserId) {
                        actionsHtml += ` <button class='btn btn-danger' onclick='withdrawTask("${task.taskId}")'>撤回任务</button>`;
                    }
                    // 仅非自己发布的PENDING任务显示"接受任务"按钮
                    if (task.status === 'PENDING' && currentUserId && task.publisherId !== currentUserId) {
                        actionsHtml += ` <button class='btn btn-accent' onclick='acceptTask("${task.taskId}")'>接受任务</button>`;
                    }
                    // 只有自己是ACCEPTED任务的接受者时显示"放弃任务"按钮
                    if (task.status === 'ACCEPTED' && currentUserId && task.acceptorId === currentUserId) {
                        actionsHtml += ` <button class='btn btn-danger' onclick='abandonTask("${task.taskId}")'>放弃任务</button>`;
                    }
                    taskItem.innerHTML = `
                        <div class='task-title'>${task.title}</div>
                        <div class='task-description'>${task.description}</div>
                        <div class='task-meta'>
                            <span>报酬: ¥${task.reward}</span>
                            <span>截止时间: ${formattedDeadline}</span>
                            <span class='task-status ${statusClass}'>${statusText}</span>
                        </div>
                        <div class='task-actions'>${actionsHtml}</div>
                    `;
                    taskList.appendChild(taskItem);
                } catch (e) {
                    showError('渲染任务时出错: ' + e.message);
                    console.error('渲染单个任务出错:', e, task);
                }
            });
        }
        function showError(message) {
            const taskList = document.getElementById('task-list');
            taskList.innerHTML = `<div class="empty-state"><div>X</div><p>${message}</p></div>`;
        }
        document.getElementById('open-publish-modal').addEventListener('click', function() {
            document.getElementById('publish-modal').style.display = 'block';
            // 设置默认截止时间为明天
            const tomorrow = new Date();
            tomorrow.setDate(tomorrow.getDate() + 1);
            tomorrow.setHours(23, 59);
            document.getElementById('task-deadline').value = tomorrow.toISOString().slice(0, 16);
            // 自动填充发布者学号
            document.getElementById('publisher-id').value = user.studentId;
        });
        document.querySelector('.close').addEventListener('click', function() {
            document.getElementById('publish-modal').style.display = 'none';
        });
        document.getElementById('cancel-publish').addEventListener('click', function() {
            document.getElementById('publish-modal').style.display = 'none';
        });
        window.addEventListener('click', function(event) {
            if (event.target == document.getElementById('publish-modal')) {
                document.getElementById('publish-modal').style.display = 'none';
            }
        });
        document.getElementById('publish-task-form').addEventListener('submit', function(e) {
            e.preventDefault();
            const formData = {
                title: document.getElementById('task-title').value,
                description: document.getElementById('task-description').value,
                reward: parseFloat(document.getElementById('task-reward').value),
                deadline: new Date(document.getElementById('task-deadline').value).toISOString()
            };
            const publisherId = user.studentId;
            fetch('/campus-help/api/tasks/publish', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Publisher-Id': publisherId
                },
                body: JSON.stringify(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('任务发布成功！');
                    document.getElementById('publish-modal').style.display = 'none';
                    document.getElementById('publish-task-form').reset();
                    fetchTasks(); // 自动刷新任务列表
                } else {
                    alert('发布失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('发布失败，请稍后再试');
            });
        });
        // 新增：发布任务后新任务插入到顶部
        function prependTaskToList(task) {
            const taskList = document.getElementById('task-list');
            const taskItem = document.createElement('div');
            taskItem.className = 'task-item';
            let statusClass = 'status-pending';
            let statusText = '待接受';
            if (task.status === 'ACCEPTED') {
                statusClass = 'status-accepted';
                statusText = '进行中';
            } else if (task.status === 'COMPLETED') {
                statusClass = 'status-completed';
                statusText = '已完成';
            }
            let deadline = new Date(task.deadline && typeof task.deadline === 'string' ? task.deadline.replace('T', ' ').replace(/-/g, '/') : task.deadline);
            let formattedDeadline;
            if (isNaN(deadline.getTime())) {
                formattedDeadline = task.deadline || '';
            } else {
                formattedDeadline = deadline.toLocaleDateString('zh-CN') + ' ' + deadline.toLocaleTimeString('zh-CN', {hour: '2-digit', minute:'2-digit'});
            }
            taskItem.innerHTML = `
                <div class='task-title'>${task.title}</div>
                <div class='task-description'>${task.description}</div>
                <div class='task-meta'>
                    <span>报酬: ¥${task.reward}</span>
                    <span>截止时间: ${formattedDeadline}</span>
                    <span class='task-status ${statusClass}'>${statusText}</span>
                </div>
                <div class='task-actions'>
                    <button class='btn' onclick='alert("任务详情功能正在开发中，任务ID: ${task.taskId}")'>查看详情</button>
                </div>
            `;
            taskList.insertBefore(taskItem, taskList.firstChild);
        }
        // 新增：接受任务功能
        function acceptTask(taskId) {
            const currentUserId = localStorage.getItem('studentId');
            if (!currentUserId) {
                alert('请先登录');
                return;
            }
            if (!confirm('确定要接受该任务吗？')) return;
            fetch(`/campus-help/api/tasks/${taskId}/accept`, {
                method: 'POST',
                headers: {
                    'Acceptor-Id': currentUserId
                }
            })
            .then(res => res.json())
            .then(data => {
                if (data.success) {
                    alert('任务接受成功！');
                    fetchTasks();
                } else {
                    alert('接受任务失败: ' + (data.message || '请稍后再试'));
                }
            })
            .catch(() => alert('接受任务失败，请稍后再试'));
        }
        // 新增：放弃任务功能
        function abandonTask(taskId) {
            const currentUserId = localStorage.getItem('studentId');
            if (!currentUserId) {
                alert('请先登录');
                return;
            }
            if (!confirm('确定要放弃该任务吗？')) return;
            fetch(`/campus-help/api/tasks/${taskId}/abandon`, {
                method: 'POST',
                headers: {
                    'Acceptor-Id': currentUserId
                }
            })
            .then(res => res.json())
            .then(data => {
                if (data.success) {
                    alert('任务已放弃！');
                    fetchTasks();
                } else {
                    alert('放弃任务失败: ' + (data.message || '请稍后再试'));
                }
            })
            .catch(() => alert('放弃任务失败，请稍后再试'));
        }
        // 新增：撤回任务功能
        function withdrawTask(taskId) {
            const currentUserId = localStorage.getItem('studentId');
            if (!currentUserId) {
                alert('请先登录');
                return;
            }
            if (!confirm('确定要撤回该任务吗？')) return;
            fetch(`/campus-help/api/tasks/${taskId}/withdraw`, {
                method: 'POST',
                headers: {
                    'Publisher-Id': currentUserId
                }
            })
            .then(res => res.json())
            .then(data => {
                if (data.success) {
                    alert('任务已撤回！');
                    fetchTasks();
                } else {
                    alert('撤回任务失败: ' + (data.message || '请稍后再试'));
                }
            })
            .catch(() => alert('撤回任务失败，请稍后再试'));
        }
        // 任务详情弹窗逻辑
        function viewTaskDetail(taskId) {
            fetch(`/campus-help/api/tasks/${taskId}`)
                .then(res => res.json())
                .then(data => {
                    if (!data.success) {
                        alert('获取任务详情失败: ' + (data.message || '请稍后再试'));
                        return;
                    }
                    const task = data.data;
                    const currentUserId = localStorage.getItem('studentId');
                    let html = `<div class='form-group'><b>标题：</b> <span id='detail-title'>${task.title}</span></div>`;
                    html += `<div class='form-group'><b>描述：</b> <span id='detail-description'>${task.description}</span></div>`;
                    html += `<div class='form-group'><b>报酬：</b> <span id='detail-reward'>¥${task.reward}</span></div>`;
                    html += `<div class='form-group'><b>截止时间：</b> <span id='detail-deadline'>${task.deadline ? new Date(task.deadline).toLocaleString('zh-CN') : ''}</span></div>`;
                    html += `<div class='form-group'><b>状态：</b> <span>${task.status}</span></div>`;
                    html += `<div class='form-group'><b>发布者学号：</b> <span>${task.publisherId}</span></div>`;
                    html += `<div class='form-group'><b>接受者学号：</b> <span>${task.acceptorId || '-'}</span></div>`;
                    // 如果是自己发布的任务，显示"修改任务"按钮
                    if (currentUserId && task.publisherId === currentUserId) {
                        html += `<div class='form-actions'><button class='btn btn-accent' onclick='editTaskDetail("${task.taskId}")'>修改任务</button></div>`;
                    }
                    document.getElementById('task-detail-body').innerHTML = html;
                    document.getElementById('task-detail-modal').style.display = 'block';
                })
                .catch(() => alert('获取任务详情失败，请稍后再试'));
        }
        document.getElementById('close-detail-modal').onclick = function() {
            document.getElementById('task-detail-modal').style.display = 'none';
        };
        window.addEventListener('click', function(event) {
            if (event.target == document.getElementById('task-detail-modal')) {
                document.getElementById('task-detail-modal').style.display = 'none';
            }
        });
        // 修改任务逻辑
        function editTaskDetail(taskId) {
            fetch(`/campus-help/api/tasks/${taskId}`)
                .then(res => res.json())
                .then(data => {
                    if (!data.success) {
                        alert('获取任务详情失败: ' + (data.message || '请稍后再试'));
                        return;
                    }
                    const task = data.data;
                    // 变为可编辑表单
                    let html = `<form id='edit-task-form'>`;
                    html += `<div class='form-group'><label>标题</label><input type='text' id='edit-title' value='${task.title}' required></div>`;
                    html += `<div class='form-group'><label>描述</label><textarea id='edit-description' required>${task.description}</textarea></div>`;
                    html += `<div class='form-group'><label>报酬</label><input type='number' id='edit-reward' value='${task.reward}' min='0' step='0.01' required></div>`;
                    html += `<div class='form-group'><label>截止时间</label><input type='datetime-local' id='edit-deadline' value='${task.deadline ? new Date(task.deadline).toISOString().slice(0,16) : ''}' required></div>`;
                    html += `<div class='form-actions'><button type='button' class='btn btn-cancel' onclick='viewTaskDetail("${task.taskId}")'>取消</button> <button type='submit' class='btn btn-primary'>保存修改</button></div>`;
                    html += `</form>`;
                    document.getElementById('task-detail-body').innerHTML = html;
                    // 表单提交
                    document.getElementById('edit-task-form').onsubmit = function(e) {
                        e.preventDefault();
                        const updateData = {
                            title: document.getElementById('edit-title').value,
                            description: document.getElementById('edit-description').value,
                            reward: parseFloat(document.getElementById('edit-reward').value),
                            deadline: new Date(document.getElementById('edit-deadline').value).toISOString()
                        };
                        fetch(`/campus-help/api/tasks/${task.taskId}/update`, {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                                'Publisher-Id': localStorage.getItem('studentId')
                            },
                            body: JSON.stringify(updateData)
                        })
                        .then(res => res.json())
                        .then(data => {
                            if (data.success) {
                                alert('修改成功！');
                                fetchTasks();
                                viewTaskDetail(task.taskId);
                            } else {
                                alert('修改失败: ' + (data.message || '请稍后再试'));
                            }
                        })
                        .catch(() => alert('修改失败，请稍后再试'));
                    };
                });
        }
    </script>
</body>
</html>